package com.neuedu.studentmanage.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.neuedu.studentmanage.entity.Attendance;
import com.neuedu.studentmanage.entity.Classroom;
import com.neuedu.studentmanage.entity.Pager;
import com.neuedu.studentmanage.entity.Result;
import com.neuedu.studentmanage.entity.Student;
import com.neuedu.studentmanage.entity.Subject;
import com.neuedu.studentmanage.service.AttendanceService;

@Controller
public class AttendanceController {

	@Autowired
	private AttendanceService attendanceService;

	@GetMapping("attendance/index")
	public String goTo() {
		return "student/attendance/attendance";
	}
	
	@PostMapping("attendance/list")
	@ResponseBody
	public Pager<Attendance> findList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "rows", defaultValue = "10") Integer rows) {
		Pager<Attendance> pager = new Pager<>(page, rows);
		List<Attendance> attendances = attendanceService.findListByPager(pager);
		Integer total = attendanceService.findTotalByPager(pager);

		pager.setRows(attendances);
		pager.setTotal(total);

		return pager;
	}
	
	@RequestMapping("attendance/add")
	@ResponseBody
	public Result doAdd(Attendance attendance,Integer stuId,Integer claroomId,Integer subId) {
		Student student = new Student();
		student.setStuId(stuId);
		attendance.setStudent(student);
		
		Classroom classroom = new Classroom();
		classroom.setClaroomId(claroomId);
		attendance.setClassroom(classroom);
		
		Subject subject = new Subject();
		subject.setSubId(subId);
		attendance.setSubject(subject);
		
		Result result = attendanceService.add(attendance);
		
		return result;
	}
}
